<?php
/**
 * Created by PhpStorm.
 * User: Monkey
 * Date: 2021/10/13
 * Time: 11:27
 * Desc: 注册
 */
declare (strict_types=1);

namespace app\api\controller;

use app\api\base\controller\Base;
use app\api\validate\RegisterValidate;

class Register extends Base
{
    /**
     * 注册
     */
    public function index()
    {
        if (request()->isPost()) {
            $validate = new RegisterValidate();
            $ret = $validate->scene('index')->check(input());
            if (!$ret) {
                return json(_E($validate->getError()));
            }

            $account = input('account', '');
            $mobile = input('mobile', '');
            $password = input('password', '');
            $payPassword = input('pay_password', '');

            $userInfo = \app\extend\model\User::where([
                ['account', '=', $account]
            ])->field('id')->find();
            if (!empty($userInfo)) {
                return json(_E('账号已经被注册'));
            }

            $userInfo = \app\extend\model\User::where([
                ['mobile', '=', $mobile]
            ])->field('id')->find();
            if (!empty($userInfo)) {
                return json(_E('手机号码已经被注册'));
            }

            $pwdArr = password($password);
            $payPwdArr = password($payPassword);

            $result = \app\extend\model\User::insert([
                'account' => $account,
                'mobile' => $mobile,
                'password' => $pwdArr['password'],
                'salt' => $pwdArr['salt'],
                'pay_password' => $payPwdArr['password'],
                'pay_salt' => $payPwdArr['salt'],
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ]);

            if (!$result) {
                return json(_E('注册失败'));
            } else {
                return json(_S('注册成功'));
            }
        }
    }
}
